<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>省市县级联</title>
</head>

<body>
  <select name="province" id="province">
    <option>---请选择省份---</option>
  </select>
  <select name="city" id="city">
    <option class="first">---请选择城市---</option>
  </select>
  <select name="area" id="area">
    <option class="first">---请选择区域---</option>
  </select>

  <script src="https://lf3-cdn-tos.bytecdntp.com/cdn/expire-1-M/jquery/3.6.0/jquery.min.js" type="application/javascript"></script>
  <script>
    $(function () {
      //页面加载完成后初始化所有的省份
      $.get('http://192.168.110.2:8888/AjaxTeach/ssxjl/getProvinces', function (result) {
        const provinces = JSON.parse(result)
        provinces.forEach(province => $('#province').append(`<option value="${province.code}">${province.name}</option>`))
      })

      //为省份下拉菜单绑定改变事件,当菜单发生改变时,加载对应的城市信息
      $('#province').change(function () {
        //清空城市列表和区域列表
        $('#city option').not('.first').remove()
        $('#area option').not('.first').remove()
        //$('#area').empty()
        $.get('http://192.168.110.2:8888/AjaxTeach/ssxjl/getCities?provinceCode=' + $(this).val(), function (result) {
          const cities = JSON.parse(result)
          cities.forEach(city => $('#city').append(`<option value="${city.code}">${city.name}</option>`))
        })
      })

      $('#city').change(function () {
        $('#area option').not('.first').remove()
        $.get('http://192.168.110.2:8888/AjaxTeach/ssxjl/getAreas?cityCode=' + $(this).val(), function (result) {
          const areas = JSON.parse(result)
          areas.forEach(area => $('#area').append(`<option value="${area.code}">${area.name}</option>`))
        })
      })
    })
  </script>
</body>

</html>